//
//  LHQDestinationDetailViewController.m
//  God_is_a_Girl
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LHQDestinationDetailViewController.h"
#import <AFNetworking.h>
#import "LHQDestinationDetailModel.h"
#import "LHQDestinationDetailTableViewCell.h"
#import "LHQButtonTableViewCell.h"
#import "LHQDestinationDetailphotoShowViewController.h"
#import "LHQDestinationHotTableViewCell.h"
#import "LHQDestinationBeautyTripViewController.h"
#import "LHQDestinationtripViewController.h"


@interface LHQDestinationDetailViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) NSMutableArray  *arr;
@property (nonatomic,retain) NSMutableDictionary *dic;
@property (nonatomic,retain) UITableView  *tableView;
@property (nonatomic,retain) LHQDestinationDetailModel  *model;
@property (nonatomic,retain) LHQButtonTableViewCell  *ButtonCell;

@end

@implementation LHQDestinationDetailViewController


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.alpha = 0;
    self.tabBarController.tabBar.frame = CGRectMake(-100, -100, 0, 0);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  datahandel];
    self.view.backgroundColor = [UIColor whiteColor];
}
//tableView视图
-(void)tableViewSubView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 8 * 7 - self.tabBarController.tabBar.frame.size.height ) style:UITableViewStylePlain];;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView  registerClass:[LHQDestinationHotTableViewCell class] forCellReuseIdentifier:@"reuse2"];
    
}
//解析接口
-(void)datahandel
{   self.arr = [NSMutableArray  array];
    self.dic = [NSMutableDictionary  dictionary];
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet  setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];//支持所有接口类型
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.detailStr];

    [manager  GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = responseObject;
        [self.arr  addObject:self.dic];
        [self.tableView  reloadData];
        [self  tableViewSubView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        self.model = [[LHQDestinationDetailModel  alloc] init];
        
        NSDictionary  *dic = [self.arr  objectAtIndex:indexPath.row];
        
        [self.model  setValuesForKeysWithDictionary:dic];
        
        LHQDestinationDetailTableViewCell *cell =  [tableView  dequeueReusableCellWithIdentifier:@"reuse"];
        if (cell == nil) {
            cell = [[LHQDestinationDetailTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
        cell.model = self.model;
        return cell;
        
    }else if(indexPath.section == 1){
        self.ButtonCell= [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
        if (self.ButtonCell == nil) {
            self.ButtonCell = [[LHQButtonTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse1"];
        }
        [self.ButtonCell.segment addTarget:self action:@selector(index:) forControlEvents:UIControlEventValueChanged];
        self.ButtonCell.label.text = @"-----热门地点-----";
        [self.ButtonCell.label setTextColor:[UIColor  lightGrayColor]];
        self.ButtonCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return self.ButtonCell;
    }
    else{
       LHQDestinationHotTableViewCell  *cell = [ tableView dequeueReusableCellWithIdentifier:@"reuse2"];
        cell.ID = self.detailStr;
        cell.Type = [self.type integerValue];
       return cell;

    }



}
//row高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return self.view.frame.size.height / 3;
    }
    else if(indexPath.section == 1)         {
            return self.view.frame.size.height / 9;
        }

    else{
        return self.view.frame.size.height*3;
        }

}




-(void)index:(UISegmentedControl *)segment
{
    if (self.ButtonCell.segment.selectedSegmentIndex == 0) {
        LHQDestinationtripViewController *trip = [[LHQDestinationtripViewController  alloc] init];
        
        
        
        trip.idStr = [[self.arr objectAtIndex:0] objectForKey:@"id"];
        NSLog(@"%@",trip.idStr);
        trip.typeStr = [[self.arr objectAtIndex:0] objectForKey:@"type"];
        
//        trip.latitude = [[[self.arr objectAtIndex:0] objectForKey:@"location"] objectForKey:@"lat"];
//        trip.longitude = [[[self.arr objectAtIndex:0] objectForKey:@"location"] objectForKey:@"lng"];

        
        
        
        [self.navigationController  pushViewController:trip animated:NO];
    }else {
        self.ButtonCell.segment.selectedSegmentIndex = 1;
        LHQDestinationBeautyTripViewController  *trip =[[LHQDestinationBeautyTripViewController  alloc] init];
        trip.type  = self.type ;
        trip.idStr = self.detailStr;
        [self.navigationController  pushViewController:trip animated:NO];
        
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LHQDestinationDetailphotoShowViewController  *show = [[LHQDestinationDetailphotoShowViewController  alloc] init];
       
        show.idStr = self.detailStr;
        show.type = self.type;
        [self.navigationController pushViewController:show animated:NO];
    
    
    }else{
        
    }
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
